package com.zhanglijie.base.sync.mardwordDetail;

import java.awt.SystemColor;

import org.openjdk.jol.info.ClassLayout;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/6/18 0018 16:21
 * synchronizer.cpp 1498
 * 注意：lambda表达式演示不了 只能通过runnable演示
 */
public class FatToSlowLockTest {
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(5000);
        /**
         * 锁降级 只发生在重量级锁降级位轻量级锁的过程
         */
        MyObject myObject = new MyObject();
        // Thread thread1 = new Thread(() -> {
        //     synchronized (myObject) {
        //         System.out.println("thread1 exe");
        //         System.out.println(ClassLayout.parseInstance(myObject)
        //             .toPrintable());
        //         try {
        //             Thread.sleep(8000);
        //         } catch (InterruptedException e) {
        //             e.printStackTrace();
        //         }
        //     }
        // });
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (myObject) {
                    System.out.println("thread1 exe");
                    System.out.println(ClassLayout.parseInstance(myObject)
                        .toPrintable());
                    try {
                        Thread.sleep(8000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        } );
        // Thread thread2 = new Thread(() -> {
        //     synchronized (myObject) {
        //         System.out.println("thread2 exe,争取偏向锁失败，可能已经竞争到重量级锁了");
        //         System.out.println(ClassLayout.parseInstance(myObject)
        //             .toPrintable());
        //         try {
        //             Thread.sleep(3000);
        //         } catch (InterruptedException e) {
        //             e.printStackTrace();
        //         }
        //     }
        // });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (myObject) {
                    System.out.println("thread2 exe,争取偏向锁失败，可能已经竞争到重量级锁了");
                    System.out.println(ClassLayout.parseInstance(myObject)
                        .toPrintable());
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread1.start();
        Thread.sleep(5000);
        thread2.start();
        thread1.join();
        thread2.join();
        Thread.sleep(1000);
        Thread thread3 = new Thread(() -> {
            synchronized (myObject) {
                System.out.println("thread3 exe");
                System.out.println(ClassLayout.parseInstance(myObject)
                    .toPrintable());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread3.start();
        thread3.join();
        System.out.println(ClassLayout.parseInstance(myObject));

    }
}
